package com.zhulin.android.edoctor.fragment;

import java.util.ArrayList;
import com.zhulin.android.edoctor.MainActivity;
import com.zhulin.android.edoctor.R;
import com.zhulin.android.edoctor.jsonobject.INGetCaseList;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList.GetCaseListModel;
import com.zhulin.android.edoctor.net.AsyncHttpPost;
import com.zhulin.android.edoctor.net.DefaultThreadPool;
import com.zhulin.android.edoctor.parsehandler.GetCaseListParseHandler;
import com.zhulin.android.edoctor.util.Command;
import com.zhulin.android.edoctor.util.EnumGetCaseList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentCaseList extends BaseFragment implements OnClickListener{

	/**
	 * 默认是经典类型
	 */
	private String CurrentListType=EnumGetCaseList.TYPICAL;
	
	/*
	 * 默认是全部类型
	 */
	private int Category=0;
	
	MainActivity mMainaActivity;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		mMainaActivity=((MainActivity)activity);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_case_content, container, false);

		initViews(view);
		loadData();
		return view;
	}

	ArrayList<JOCaseInfoModel> arrayList;
	ListView lstViewCaseList=null;
	MySimpleAdapter mySimpleAdapter;
	private Button btnCaseListTypeClassics,btnCaseListTypeNew,btnCaseListTypePopularity;
	private void initViews(View view){
		lstViewCaseList=(ListView)view.findViewById(R.id.lstVewCaseList);
		btnCaseListTypeClassics=(Button)view.findViewById(R.id.btnCaseListTypeClassics);
		btnCaseListTypeNew=(Button)view.findViewById(R.id.btnCaseListTypeNew);
		btnCaseListTypePopularity=(Button)view.findViewById(R.id.btnCaseListTypeHottest);
		
		view.findViewById(R.id.txtMore).setOnClickListener(this);

		btnCaseListTypeClassics.setOnClickListener(this);
		btnCaseListTypeNew.setOnClickListener(this);
		btnCaseListTypePopularity.setOnClickListener(this);
		
		
	}

	private void loadData(){
		mySimpleAdapter=new MySimpleAdapter(mData);
		lstViewCaseList.setAdapter(mySimpleAdapter);
		
		lstViewCaseList.setOnScrollListener(scrollListener);
		lstViewCaseList.setOnItemClickListener(onItemClickListener);
		reLoadData(0, EnumGetCaseList.TYPICAL, 10, 1);
	}
	
	private OnItemClickListener onItemClickListener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			mMainaActivity.addFragmentOfCaseDetail(mData.get(position));
		}
	};
	
	private OnScrollListener scrollListener=new OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if(firstVisibleItem+visibleItemCount==totalItemCount){
			}
		}
	};
	
	/**
	 * 类别,0全部,1-5按顺利
	 * @param Category 大类别,经典,最新,人气
	 * @param listType
	 * @param count
	 * @param offset
	 */
	private void reLoadData(int Category,String listType,int count,int offset){
		
		
		ShowProgressDialog("", getResources().getString(R.string.requesting));
		INGetCaseList inGetCaseList=new INGetCaseList();
		inGetCaseList.setCategory(Category);
		inGetCaseList.setListType(listType);
		inGetCaseList.setCount(count);
		inGetCaseList.setOffset(offset);
		
		AsyncHttpPost asyncHttpPost=new AsyncHttpPost(new GetCaseListParseHandler(), Command.SERVER_ADDRESS+Command.ACTION_GetCaseList, 
				inGetCaseList.getClassInfo(), requestResultCallback);
		DefaultThreadPool.getInstance().execute(asyncHttpPost);
		listRequest.add(asyncHttpPost);
	}
	
	private ArrayList<GetCaseListModel> mData=new ArrayList<GetCaseListModel>();
	@Override
	public void onSuccess(Object o) {
		super.onSuccess(o);
		OUTGetCaseList outGetCaseList=(OUTGetCaseList) o;
		if(outGetCaseList.getResult()==1){
			//Toast(getActivity(), "获取成功", Toast.LENGTH_LONG);
			
			ArrayList<GetCaseListModel> arrayListTemp=outGetCaseList.getCaseList();
			if(arrayListTemp!=null)
			{
				mData.addAll(arrayListTemp);
			}
			
			mySimpleAdapter.notifyDataSetInvalidated();
		}else{
			Toast(getActivity(), outGetCaseList.getDescription(), Toast.LENGTH_LONG);
		}
	}
	
	@Override
	public void onFail(Exception e) {
		super.onFail(e);
	}

	private class MySimpleAdapter extends BaseAdapter{

		ArrayList<GetCaseListModel> mData;
		public MySimpleAdapter(ArrayList<GetCaseListModel> getCaseListModels){
			this.mData=getCaseListModels	;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView=LayoutInflater.from(getActivity()).inflate(R.layout.item_caselist_subitem, parent,false);
			}

			TextView txtName		=			(TextView)convertView.findViewById(R.id.txtName);
			TextView txtPraiseCount	=			(TextView)convertView.findViewById(R.id.txtPraiseCount);
			TextView txtCommentCount=			(TextView)convertView.findViewById(R.id.txtCommentCount);
			TextView txtUpdateDate	=			(TextView)convertView.findViewById(R.id.txtUpdateDate);



			GetCaseListModel joCaseInfoModel=mData.get(position);
			txtName.setText(joCaseInfoModel.getCaseTitle());
			txtPraiseCount.setText("赞:"+joCaseInfoModel.getLaudCount()+"个");
			txtCommentCount.setText("评:"+joCaseInfoModel.getCommentCount()+"个");


			//String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(joCaseInfoModel.getUpdateDate());
			txtUpdateDate.setText("上传时间:"+joCaseInfoModel.getUploadDate());

			return convertView;
		}

	}

	@Override
	public void onClick(View v) {
		setButtonBackground(v);

		switch(v.getId()){
		case R.id.btnCaseListTypeClassics:
		{
			break;
		}case R.id.btnCaseListTypeNew:{
			break;
		}case R.id.btnCaseListTypeHottest:{
			break;
		}case R.id.txtMore:{
			reLoadData(Category, CurrentListType, 10, mySimpleAdapter.getCount());
			break;
		}
		}
	}

	private void setButtonBackground(View v){
		
		switch(v.getId()){
		case R.id.btnCaseListTypeClassics:
		{
			showCaseTypeList(v);
			break;
		}case R.id.btnCaseListTypeNew:{
			showCaseTypeList(v);
			break;
		}case R.id.btnCaseListTypeHottest:{
			showCaseTypeList(v);
			break;
		}
		}

		
	}
	
	PopupWindow mPopupWindow;
	private void showCaseTypeList(View anchor){
		View mViewPopup=LayoutInflater.from(getActivity()).inflate(R.layout.popup_case_type, null);
		mPopupWindow = new PopupWindow(mViewPopup, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_title_background));
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(null);
		mPopupWindow.update();
		mPopupWindow.showAsDropDown(anchor);
		
		mViewPopup.setTag(anchor);//将类别VIEW添加到tag,以便区分大类别:经典,最新,人气等
		addPopupItemClickListener(mViewPopup);
	}
	
	private void addPopupItemClickListener(View v){
		
		View view0=v.findViewById(R.id.btnCaseType0);
		View view1=v.findViewById(R.id.btnCaseType1);
		View view2=v.findViewById(R.id.btnCaseType2);
		View view3=v.findViewById(R.id.btnCaseType3);
		View view4=v.findViewById(R.id.btnCaseType4);
		View view5=v.findViewById(R.id.btnCaseType5);
		
		view0.setTag(R.id.tag_first, view0.getTag());//第一个保存当前VIEW的tag :0-5
		view0.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view1.setTag(R.id.tag_first, view1.getTag());//第一个保存当前VIEW的tag :0-5
		view1.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view2.setTag(R.id.tag_first, view2.getTag());//第一个保存当前VIEW的tag :0-5
		view2.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view3.setTag(R.id.tag_first, view3.getTag());//第一个保存当前VIEW的tag :0-5
		view3.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view4.setTag(R.id.tag_first, view4.getTag());//第一个保存当前VIEW的tag :0-5
		view4.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view5.setTag(R.id.tag_first, view5.getTag());//第一个保存当前VIEW的tag :0-5
		view5.setTag(R.id.tag_second, v.getTag());//第二个保存父窗口的VIEW
		
		view0.setOnClickListener(clickListenerCaseType);
		view1.setOnClickListener(clickListenerCaseType);
		view2.setOnClickListener(clickListenerCaseType);
		view3.setOnClickListener(clickListenerCaseType);
		view4.setOnClickListener(clickListenerCaseType);
		view5.setOnClickListener(clickListenerCaseType);
	}
	
	private OnClickListener clickListenerCaseType=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			mData.clear();
			mySimpleAdapter.notifyDataSetInvalidated();
			if(mPopupWindow!=null && mPopupWindow.isShowing()){
				mPopupWindow.dismiss();
			}
			switch(v.getId()){
			case R.id.btnCaseType1:{
				break;
			}
			case R.id.btnCaseType2:{
				break;
			}
			case R.id.btnCaseType3:{
				break;
			}
			case R.id.btnCaseType4:{
				break;
			}
			case R.id.btnCaseType5:{
				break;
			}
			case R.id.btnCaseType0:{
				break;
			}
			}
			
			startRequest(v);
		}
	};
	
	private void startRequest(View v){
		View vParent= (View) v.getTag(R.id.tag_second);
		if(vParent.getId()==R.id.btnCaseListTypeClassics){
			CurrentListType=EnumGetCaseList.TYPICAL;
		}else if(vParent.getId()==R.id.btnCaseListTypeNew){
			CurrentListType=EnumGetCaseList.NEWEST;
		}else if(vParent.getId()==R.id.btnCaseListTypeHottest){
			CurrentListType=EnumGetCaseList.HOTTEST;
		}
		
		Category=Integer.valueOf(v.getTag(R.id.tag_first).toString());
		
		int offset=0;
		if(mySimpleAdapter!=null){
			offset=mySimpleAdapter.getCount();
		}
		reLoadData(Category, CurrentListType, 10, offset);
	}
}
